<?php
/**
 * @author Mingming Tang iamalazyrat@gmail.com
 */

class Tag_Controller extends Base_Controller {

  public $restful = true;

  public function get_index() {
    return Redirect::to(URL::to('tag/list'));
  }

  public function get_list() {
    $collection_id = Input::get('collection_id', false);
    if (!$collection_id) {
      $collections = Collection::with('tags')->all();
    } else {
      $collections[] = Collection::with('tags')->find($collection_id);
    }
    return View::make('tag.list.main')->with('collections', $collections);
  }

  public function get_manage() {
    $collection_id = Input::get('collection_id', false);
    $collection = Collection::with('tags')->find($collection_id);
    if (!$collection) {
      return View::make('tag.manage.not_found');
    } else {
      return View::make('tag.manage.main')->with('collection', $collection);
    }
  }

  public function post_delete() {
    $data = Input::all();
    if (!$data OR !isset($data['tags']) OR !isset($data['collection_id'])) return Response::json(array('code' => 400));
    $tags = $data['tags'];
    if ($tags) {
      $tags_obj = Tag::where_in('name', $tags)->where('collection_id', '=', $data['collection_id'])->get();
      $tags_id = array();
      foreach ($tags_obj as $t) $tags_id[] = $t->id;
      Tag::where_in('name', $tags)->where('collection_id', '=', $data['collection_id'])->delete();
      DB::table('img_tag')->where_in('tag_id', $tags_id)->delete();
      return Response::json(array('code'=>200));
    }
    return Response::json(array('code'=>400));
  }

  public function get_group() {
    $collection_id = Input::get('collection_id', false);
    $collection = Collection::with('tags')->find($collection_id);
    if (!$collection) {
      return View::make('tag.group.not_found');
    } else {
      return View::make('tag.group.main')->with('collection', $collection);
    }
  }

  public function get_new_group() {
    $collection_id = Input::get('collection_id', false);
    $collection = Collection::with('tags')->find($collection_id);
    if (!$collection) {
      return View::make('tag.group.not_found');
    } else {
      return View::make('tag.new_group.main')->with('collection', $collection);
    }
  }

  public function post_new_group() {
    $input = Input::all();
    $rules = array(
      'name'  => 'required|max:1024',
      'collection_id'  => 'required',
      'tags' => 'max:1024',
    );
    $messages = array(
      'name_required' => '忘记写标签组名称了喔...',
      'name_max' => '标签组名称太长了喔...',
      'collection_required' => '没有找到对应的专题喔...',
      'tags_max' => 'Tags太长了喔...',
    );
    $collection = Collection::find($input['collection_id']);
    $validation = Validator::make($input, $rules, $messages);
    if ($validation->fails()) {
      return View::make('tag.new_group.fail')->with('error', $validation->errors)->with('collection',$collection);
    } else {
      Group::create($input);
      return View::make('tag.new_group.succ')->with('collection', $collection);
    }
  }

  public function post_delete_group() {
    $data = Input::all();
    if (!$data OR !isset($data['group_id'])) return Response::json(array('code'=>400));
    $group = Group::find($data['group_id']);
    if ($group) $group->delete();
    return Response::json(array('code'=>200));
  }

  function post_edit_group() {
    $data = Input::all();
    if (!isset($data['tags']) OR !isset($data['collection_id']) OR !isset($data['group_id'])) {
      return Response::json(array('code', '400'));
    }
    $tags = explode(',', $data['tags']);
    $tags = implode(',', array_unique($tags));
    $group = Group::find($data['group_id']);
    if (!$group) return Response::json(array('code', 400));
    $group->tags = $tags;
    $group->save();
    return Response::json(array('code'=>200));
  }

  function post_batch_tag() {
    $data = Input::all();
    if (!isset($data['tags']) OR !isset($data['collection_id'])) {
      return Response::json(array('code', '400'));
    }
    $tags = explode(',', $data['tags']);
    $tags = array_unique($tags);
    $imgs = Img::with('product')->where('collection_id', '=', $data['collection_id'])->get();
    foreach ($tags as $t) {
      $products = Product::with('imgs')->where('title', 'like', "%{$t}%")->get();
      $tag = Tag::where('name', '=', $t)->where('collection_id', '=', $data['collection_id'])->first();
      if (!$tag) $tag = Tag::create(array('collection_id'=>$data['collection_id'], 'name'=>$t));
      foreach ($products as $p) {
        if (!DB::table('collection_product')->where('product_id', '=', $p->id)->where('collection_id', '=', $data['collection_id'])->first()) continue;
        foreach ($p->imgs as $i) {
          if (!DB::table('img_tag')->where('tag_id', '=', $tag->id)->where('img_id', '=', $i->id)->first()) {
            DB::table('img_tag')->insert(array('tag_id'=>$tag->id, 'img_id'=>$i->id));
          }
        }
      }
    }
    return Response::json(array('code'=>200));
  }

}
